/**  
* @Title: AppPush.java
* @Package com.lemon.utils.appPush
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年11月21日
* @version V1.0  
*/
package com.lemon.utils.appPush;

import java.io.IOException;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IAliasResult;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.ITemplate;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.base.payload.APNPayload.DictionaryAlertMsg;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.lemon.utils.StringUtil;


/**
 * <p>Title: AppPush</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年11月21日
 * @版本 V 1.0 
 */
public class AppPush {
	//定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
	private static String appId = "BZktouBiGf586f8nT05Dm9";
	private static String appKey = "vLTVUTZoPJ6weoT0i4I858";
	private static String masterSecret = "OWLmw76TZEADCKUA0gNxr4";
	private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
	private static String Alias = "cityRunAlias";
	public static void main(String[] args) throws IOException {

		//		String cid = "3774d22f01b6275835ce9a49a342a7aa";
		String cid = "2ce5310a97cfeeb49a315908eb162a59";
		AppPush push = new AppPush();
		push.setCid(cid);
		push.setContent("2018128897898950");
		push.setTitle("新订单通知");
		push.setType(11);
		push.setText("您有一个新订单,点击查看");
		push.setOrderType(1);
		IPushResult ret = AppPush.pushMsgSingle(push , "456789");
		System.out.println(ret.getResponse().toString());
	}

	public static boolean bindAlias(String cid) {
		IGtPush push = new IGtPush(url, appKey, masterSecret);
		IAliasResult bindSCid = push.bindAlias(appId , Alias , cid);
		System.out.println("绑定结果：" + bindSCid.getResult() + "错误码:" + bindSCid.getErrorMsg());
		return bindSCid.getResult();
	}
	/**
	 * <b>Description:<blockquote> 此方法封装了消息类型, 推荐使用
	 * </blockquote></b>
	 * 
	 * @Title: pushMsgToSingle
	 * @param appPush
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月24日
	 */
	public static IPushResult pushMsgToSingle(AppPush appPush) {
		if (appPush == null) {
			return null;
		}
		return pushMsgSingle(appPush);
	}

	public static IPushResult pushMsgSingle(AppPush appPush) {
		return pushMsgSingle(appPush , "");
	}
	public static IPushResult pushMsgSingle(AppPush appPush , String tcinfo) {

		////1=订单消息 11=订单取消  2=桌台消息   3=系统类型 4=提现成功 41=提现被拒绝
		JSONObject js = new JSONObject();
		js.put("msgType" , appPush.getType());
		String title = "新订单";
		switch (appPush.getType()) {
		case 1:
			title = "新订单";
			if (appPush.getOrderType() == 1) {
				js.put("content" , "您有新的城跑外卖订单,请及时处理");
			} else {
				js.put("content" , "您有新的城跑堂食订单,请及时处理");
			}
			break;
		case 11:
			title = "取消订单";
			js.put("content" , "您有订单被取消,请及时处理");
			break;
		case 12:
			title = "新订单";
			if (appPush.getOrderType() == 1) {
				js.put("content" , "您有新的城跑外卖订单,请及时处理");
			} else {
				js.put("content" , "您有新的城跑堂食订单,请及时处理");
			}
			break;
		case 2:
			title = "服务消息";
			js.put("content" , appPush.getContent());
			break;
		case 3:
			title = "系统消息";
			js.put("content" , "");
			break;
		case 4:
			title = "提现成功";
			js.put("content" , "您有新的提现消息,请及时查看");
			break;
		case 41:
			title = "提现拒绝";
			js.put("content" , "您有新的提现消息,请及时查看");
			break;
		default:
			js.put("content" , "您有新的城跑外卖订单,请及时处理");
			break;
		}
		js.put("title" , title);
		js.put("orderId" , appPush.getContent());
		js.put("orderType" , appPush.getOrderType());
		ITemplate iosTransmissionTemplate = null;
		if (!"".equals(tcinfo)) {
			iosTransmissionTemplate = iosTransmissionTemplate2(appPush.getTitle() , JSONObject.toJSONString(js) , "1" ,
					tcinfo);// 带APNPayload
		} else {
			iosTransmissionTemplate = iosTransmissionTemplate(appPush.getTitle() , JSONObject.toJSONString(js) , "1");// 带APNPayload
		}
		return pushMsgToSingle(appPush.getCid() , iosTransmissionTemplate , false);
	}

	// 单个推送
	private static IPushResult pushMsgToSingle(String Cid , ITemplate template , boolean offline) {
		IGtPush push = new IGtPush(appKey, masterSecret, true);

		SingleMessage message = new SingleMessage();

		message.setOffline(offline);
		// 离线有效时间，单位为毫秒，可选
		message.setOfflineExpireTime(24 * 3600 * 1000);
		message.setData(template);
		// 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
		message.setPushNetWorkType(0);
		Target target = new Target();
		target.setAppId(appId);
		target.setClientId(Cid);
		IPushResult ret = null;

		try {
			ret = push.pushMessageToSingle(message , target);
		} catch (RequestException e) {
			e.printStackTrace();
			ret = push.pushMessageToSingle(message , target , e.getRequestId());
		}
		return ret;
	}
	// 对单个用户推送消息
	/*
	 * 1. cid
	 * 2. 要传到客户端的 msg
	 * 2.1 标题栏：key = title,
	 * 2.2 通知栏内容： key = titleText,
	 * 2.3 穿透内容：key = transText
	 */
	public static IPushResult pushMsgToSingle(String cid , Map<String , String> msg) {
		if (StringUtil.isEmpty(cid)) {
			return null;
		}
		// 代表在个推注册的一个 app，调用该类实例的方法来执行对个推的请求
		IGtPush push = new IGtPush(url, appKey, masterSecret);
		// 创建信息模板
		NotificationTemplate template = getNotifacationTemplate(appId , appKey , msg);
		//定义消息推送方式为，单推
		SingleMessage message = new SingleMessage();
		// 设置推送消息的内容
		message.setData(template);
		message.setOffline(true);
		// 离线有效时间，单位为毫秒，可选
		message.setOfflineExpireTime(24 * 3600 * 1000);
		message.setData(template);
		// 设置推送目标
		Target target = new Target();
		target.setAppId(appId);
		// 设置cid
		target.setClientId(cid);

		// 获得推送结果
		IPushResult result = push.pushMessageToSingle(message , target);
		/*
		 * 1. 失败：{result=sign_error}
		 * 2. 成功：{result=ok, taskId=OSS-0212_1b7578259b74972b2bba556bb12a9f9a, status=successed_online}
		 * 3. 异常
		 */
		return result;
	}

	// 设置通知消息模板
	/*
	 * 1. appId
	 * 2. appKey
	 * 3. 要传送到客户端的 msg
	 * 3.1 标题栏：key = title,
	 * 3.2 通知栏内容： key = titleText,
	 * 3.3 穿透内容：key = transText
	 */
	private static NotificationTemplate getNotifacationTemplate(String appId , String appKey ,
			Map<String , String> msg) {
		// 在通知栏显示一条含图标、标题等的通知，用户点击后激活您的应用
		NotificationTemplate template = new NotificationTemplate();
		// 设置appid，appkey
		template.setAppId(appId);
		template.setAppkey(appKey);
		// 穿透消息设置为，1 强制启动应用
		template.setTransmissionType(1);
		// 设置穿透内容
		System.out.println(msg.get("title") + "::" + msg.get("text") + "::" + msg.get("trans"));
		template.setTransmissionContent(msg.get("trans"));
		// 设置style
		Style0 style = new Style0();
		// 设置通知栏标题和内容
		style.setTitle(msg.get("title"));
		style.setText(msg.get("text"));
		// 设置通知，响铃、震动、可清除
		style.setRing(true);
		style.setVibrate(true);
		style.setClearable(true);
		// 设置
		template.setStyle(style);

		return template;
	}


	/////////////////////////////////////////
	/**
	 * ios透传，设置APNPayload参数
	 * 附带特殊参数
	 */
	private static TransmissionTemplate iosTransmissionTemplate2(String title , String messageInfo , String badge ,
			String parkinfo) {
		TransmissionTemplate template = transmissionTemplate(messageInfo);
		//APNPayload payload = getApnPayload(title, messageInfo, badge);
		APNPayload payload = getApnPayload4park(title , messageInfo , badge , parkinfo);
		// 字典模式使用下者
		// payload.setAlertMsg(getDictionaryAlertMsg());
		template.setAPNInfo(payload);
		return template;
	}

	// 透传消息
	private static TransmissionTemplate transmissionTemplate(String messageInfo) {
		TransmissionTemplate template = new TransmissionTemplate();
		template.setAppId(appId);
		template.setAppkey(appKey);
		template.setTransmissionContent(messageInfo);
		template.setTransmissionType(2);
		return template;
	}

	private static APNPayload getApnPayload4park(String title , String messageInfo , String badge , String parkinfo) {
		APNPayload payload = new APNPayload();
		// +1在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
		payload.setAutoBadge(badge);
		payload.setContentAvailable(1);
		payload.setSound("123.wav");
		payload.setCategory("$由客户端定义");
		// //简单模式APNPayload.SimpleMsg
		// payload.setAlertMsg(new APNPayload.SimpleAlertMsg(messageInfo));
		DictionaryAlertMsg dictionaryAlertMsg = new DictionaryAlertMsg();
		dictionaryAlertMsg.setTitle(title);
		dictionaryAlertMsg.setBody(messageInfo);
		payload.setAlertMsg(dictionaryAlertMsg);
		payload.addCustomMsg("parkinfo" , parkinfo);
		return payload;
	}

	private static TransmissionTemplate iosTransmissionTemplate(String title , String messageInfo , String badge) {
		TransmissionTemplate template = transmissionTemplate(messageInfo);
		APNPayload payload = getApnPayload(title , messageInfo , badge);
		// 字典模式使用下者
		// payload.setAlertMsg(getDictionaryAlertMsg());
		template.setAPNInfo(payload);
		return template;
	}

	private static APNPayload getApnPayload(String title , String messageInfo , String badge) {
		APNPayload payload = new APNPayload();
		// +1在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
		payload.setAutoBadge(badge);
		payload.setContentAvailable(1);
		payload.setSound("123.wav");
		payload.setCategory("$由客户端定义");
		// //简单模式APNPayload.SimpleMsg
		// payload.setAlertMsg(new APNPayload.SimpleAlertMsg(messageInfo));
		DictionaryAlertMsg dictionaryAlertMsg = new DictionaryAlertMsg();
		dictionaryAlertMsg.setTitle(title);
		dictionaryAlertMsg.setBody(messageInfo);
		payload.setAlertMsg(dictionaryAlertMsg);
		//payload.addCustomMsg("parkinfo", "{\"parkid\":\"停车场id\",\"platenum\":\"车位号\",\"statue\":\"状态\"}");
		return payload;
	}

	private Integer type; //1=订单消息 11=订单取消  12=自动接单 2=桌台消息   3=系统类型 4=提现成功 41=提现被拒绝 
	private String content;
	private String title;
	private String text;
	private String trans;
	private String cid;
	private Integer orderType = 0;

	public Integer getType() {
		return this.type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getText() {
		return this.text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getTrans() {
		return this.trans;
	}

	public void setTrans(String trans) {
		this.trans = trans;
	}

	public String getCid() {
		return this.cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public Integer getOrderType() {
		return this.orderType;
	}

	public void setOrderType(Integer orderType) {
		this.orderType = orderType;
	}

}
